<template>
    
    <!-- 组件的模板 -->
    <div class="page form-report show-tabbar">
        <empty :show-empty="form_list && form_list.length == 0" :msg="'暂无表单,点击右上角创建一个吧！'"></empty>
        <div class="mod-form com-border-top" v-for="form in form_list">
            <div class="topbar com-border-bottom">
                <h2>{{form.title}}</h2>
                <div class="state">{{ form.status == 1 ? '进行中' : '已禁用' }}<input class="weui-switch" type="checkbox" v-model="form.status" :true-value="1" :false-value="-1" @change="changeStatus($index, form.id, form.status)"></div>
            </div>            
            <ul class="opt-box">
                <li @click="gotoEditform(form.id, $index, form.status)"><a class="touch" href="javascript:;"><span class="icon icon_1 bg_sprite"></span>编辑</a></li>
                <li @click="gotoSetupforminfo(form.id)"><a class="touch" href="javascript:;"><span class="icon icon_2 bg_sprite"></span>设置</a></li>
                <li @click="shareForm(form)"><a class="touch" href="javascript:;"><span class="icon icon_3 bg_sprite"></span>分享</a></li>
                <li @click="delForm($index, form.id, -5)"><a class="touch" href="javascript:;"><span class="icon icon_4 bg_sprite"></span>删除</a></li>
            </ul>            
        </div>
        <!-- <a class="add-item touch" href="javascript:;" @click="gotoSelectTemp"><span class="icon bg_sprite"></span><span>新增信息上报表单</span></a> -->
    </div>


    <!-- <div class="weui-cells" v-for="form in form_list">
        <div class="weui-cell">
            <div class="weui-cell__bd">
                <p class="formname">{{form.title}}</p>
                <div class="weui-flex">
                    <div class="weui-flex__item">
                        <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default ver-ali-mid" @click="gotoEditform">编辑</a>
                    </div>
                    <div class="weui-flex__item">
                        <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default ver-ali-mid" @click="gotoSetupforminfo">设置</a>
                    </div>
                    <div class="weui-flex__item">
                        <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default ver-ali-mid">禁用</a>
                    </div>
                    <div class="weui-flex__item">
                        <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default ver-ali-mid">分享</a>
                    </div>
                    <div class="weui-flex__item">
                        <a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default ver-ali-mid">删除</a>
                    </div>
                </div>
            </div>
        </div>
    </div> -->

    <!-- <div class="weui-cells">
        <div class="weui-cell addnewform" @click="gotoSelectTemp">
            <div class="weui-cell__bd">
                <p><span>+</span>新增信息上报表单</p>
            </div>
        </div>
    </div> -->
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    // commonPage.canPullWebView(true)
    import clientUi from 'gapi/clientUi.js'
    import socialApi from 'clientApi/socialApi.js'
    import clientInfo from 'gapi/clientInfo.js'
    import widget from 'gapi/widget'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import Empty from 'components/Empty/Empty'

    /**
     *  组件实例对象
     */
    export default {
        components: {
            Empty
        },
        data: function(){
            return{
                // form_list: [
                    // {
                    //     id: "3",
                    //     title: "title0000",
                    //     status: "1",
                    //     project_id: "444"
                    // }
                // ],
                form_list: '', //[]
                user_info: {}
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                commonPage.loadPageStatus('loading')

                cache.getCache('user_info', (data) => {
                    this.user_info = data
                    util.ajax({
                        url: '/form/project/list',
                        type: 'GET',
                        data: {
                            team_id: data.team_id,
                            project_id: data.project_id
                        },
                        timeout: 10000,
                        success: (data) => {
                            // success
                            console.log(data.data);
                            if(data.errno == 0){
                                this.form_list = data.data
                            }else{
                                // widget.toast('服务器异常')
                                widget.toast(data.errmsg)
                            }
                            commonPage.loadPageStatus('loadSuccess')
                            // alert(JSON.stringify(data))
                        },
                        error: (xhr, status) => {
                            widget.toast('服务器异常')
                            commonPage.loadPageStatus('loadSuccess')
                            // error
                        },
                        noNetwork: () => {
                            // 无网络的回调
                            widget.toast('网络连接失败，请检查网络')
                            commonPage.loadPageStatus('loadSuccess')
                        }
                    })
                })

                window.loadData = () => {
                    // commonPage.dataDownloadFinished()
                    cache.getCache('user_info', (data) => {
                        util.ajax({
                            url: '/form/project/list',
                            type: 'GET',
                            data: {
                                team_id: data.team_id,
                                project_id: data.project_id
                            },
                            timeout: 10000,
                            success: (data) => {
                                // success
                                if(data.errno == 0){
                                    this.form_list = data.data
                                }else{
                                    // widget.toast('服务器异常')
                                    widget.toast(data.errmsg)
                                }
                                // alert(JSON.stringify(data))
                            },
                            error: (xhr, status) => {
                                // error
                                widget.toast('服务器异常')
                            },
                            noNetwork: () => {
                                // 无网络的回调
                                widget.toast('网络连接失败，请检查网络')
                            }
                        })
                    })
                }
            },
            gotoSelectTemp (){
                location.href = `dmsaas://SelectFormTemp`
            },
            gotoEditform (id, index, originalStatus){
                this.changeStatus(index, id, -1)
                console.log(id)
                console.log(index)
                console.log(originalStatus)
                let encodeStr = encodeURIComponent(`form_id=${id}&originalStatus=${originalStatus}`)
                location.href = `dmsaas://Editform?urdData=${encodeStr}`
            },
            gotoSetupforminfo (id){
                //获取设置信息
                let encodeStr = encodeURIComponent(`form_id=${id}&type=2`)
                location.href = `dmsaas://SetupFormInfo?urdData=${encodeStr}`
            },
            changeStatus (index, form_id, status){//更改表单状态或删除表单
                // alert(form_id)
                // alert(status)
                if(status != -5){
                    this.form_list[index].status = status
                }
                cache.getCache('user_info', (data) => {
                    // this.team_id = data.team_id
                    // this.project_id = data.project_id
                    util.ajax({
                        url: '/form/status',
                        type: 'POST',
                        data: {
                            team_id: data.team_id,
                            project_id: data.project_id,
                            form_id: form_id,
                            status: status
                        },
                        timeout: 10000,
                        success: (data) => {
                            // success
                            console.log(data)
                            if(data.errno == 0){
                                if(status == -5){
                                    this.form_list.splice(index, 1)
                                    widget.toast('删除成功')
                                }
                            }else if(data.errno == 15011){
                                widget.toast(data.errmsg)
                                this.form_list[index].status = -1
                            }else{
                                // widget.toast('服务器异常')
                                widget.toast(data.errmsg)
                                // alert(JSON.stringify(data))
                            }
                        },
                        error: (xhr, status) => {
                            // error
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            // 无网络的回调
                            widget.toast('网络连接失败，请检查网络')
                            this.form_list[index].status = -status
                        }
                    })
                })
            },
            delForm(index, form_id, status) {
                clientUi.showDialog('提醒', '表单收集的内容将一起删除无法恢复，是否确认删除?', (statusData) => {
                    // alert(statusData.status)
                    if(statusData.status == 1){
                        // alert(status)
                        this.changeStatus (index, form_id, status)
                    }
                }, '确认', '取消')
            },
            shareForm (form){
                // commonPage.loadPageStatus('loading')
                

                util.ajax({
                    url: '/form/info/share',
                    data: {
                        team_id:this.user_info.team_id,
                        project_id: this.user_info.project_id,
                        form_id: form.id
                    },
                    success: (data) => {
                        // alert(JSON.stringify(data.data.logo))
                        if(data.errno==0){
                            let params = `?team_id=`+this.user_info.team_id + "&project_id="+this.user_info.project_id + "&form_id="+form.id;

                            let shareHref = data.data.url +params;
                            // alert(shareHref)
                            socialApi.shareKQTask({
                                title: data.data.title,
                                text: data.data.text,
                                logo: data.data.logo,
                                url: shareHref
                            })
                        }else{
                          // widget.toast('服务器异常')
                          widget.toast(data.errmsg)
                        }
                        // commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                          widget.toast('服务器异常')
                         // commonPage.loadPageStatus('loadSuccess')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
        },
        created(){

            this.init();
            commonPage.updateTitleBar({
                //backAction: 'checkChange',    // 点击返回按钮的动作（即回调函数名称）
                // leftDisplay: false,            // 是否显示左按钮
                // leftText: '',                // 左按钮文案
                //leftAction: 'checkChange',    // 点击左按钮的动作（即回调函数名称）
                rightDisplay: true,            // 是否显示右按钮
                rightText: '新增表单',            // 右按钮文案
                rightAction: 'saveResume'    // 点击右按钮的文案（即回调函数名称）
            })
            window.saveResume = this.gotoSelectTemp

        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/form-report.css"></style>




